#include "lcd.h"

unsigned long LCD_BASE = 0xbf040000;
#define GEN_POINT(x, y) ((x) & 0xffff) | ((y) << 16)

void lcd_write(int addr, unsigned val) {
    *((volatile unsigned int *)(LCD_BASE+(addr<<2))) = val;
}

void lcd_set_window(unsigned x1, unsigned y1, unsigned x2, unsigned y2) {
    unsigned lt = GEN_POINT(x1, y1);
    unsigned rb = GEN_POINT(x2, y2);
    lcd_write(0, lt);
    lcd_write(1, rb);
}

void lcd_set_circle(unsigned x1, unsigned y1) {
    unsigned lt = GEN_POINT(x1-5,y1-5);
    lcd_write(2, lt);
}

void lcd_set_rect(unsigned x1, unsigned y1, unsigned x2, unsigned y2) {
    unsigned lt = GEN_POINT(x1, y1);
    unsigned rb = GEN_POINT(x2, y2);
    lcd_write(3, lt);
    lcd_write(4, rb);
}

void lcd_set_color(unsigned color) {
    lcd_write(5, color);
}

void lcd_start() {
    lcd_write(6, 1);
}

int lcd_busy() {
    return (*((volatile unsigned int *)(LCD_BASE+24)));
}